﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Telerik.Windows.Controls;
using BrokerBase.Lib.Product;
using System.Windows;
using BrokerBase.Lib.Policy;
using System.Collections.ObjectModel;


namespace Broker.View.PolicyView
{
    public class PolicyTabItemModel : INotifyPropertyChanged
    {
        private PolicyBaseControl contentControl;

        private bool isSelected;

        private bool isEnabled;

        private ObservableCollection<PolicyTabItemModel> tabItems; 

        public PolicyTabItemModel(PolicyBaseControl pbc,ObservableCollection<PolicyTabItemModel> tabItems)
        {
            this.contentControl = pbc;
            this.tabItems = tabItems;
            this.IsCloseBtnEnabled =!pbc.PolicyLight.Owner.IsCheck;

            this.DelCommand = new DelegateCommand(
                delegate
                {
                    IPolicyLight pl=contentControl.PolicyLight;
                    IScheme scheme = pl.Owner;
                    if (pl.Policy != null && pl.Policy.ProductType == ProductType.Basic && scheme.PolicyLights.Count>1)
                    {
                        if (MessageBox.Show("删除主险同时也要删除所有的附加险，是否确认?","提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            tabItems.Clear();
                            scheme.PolicyLights.Clear();
                            contentControl.OnPolicyChanged();
                        }
                    }else if (MessageBox.Show("是否删除【" + pl.ProductName + "】?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        scheme.PolicyLights.Remove(pl);
                        
                        tabItems.Remove(this);
                        contentControl.OnPolicyChanged();
                    }
                });

        }

        public ProductType ProductType
        {
            get { return contentControl.PolicyLight.ProductType; }
        }

        public string HeaderTitle
        {
            get
            {
                string title=null;
                if(this.contentControl.PolicyLight.Policy==null)
                {
                    title="自定义险";
                }else
                {
                    if(this.contentControl.PolicyLight.Policy.ProductType==ProductType.Basic)
                    {
                        title="主险";
                    }else if(this.contentControl.PolicyLight.Policy.ProductType==ProductType.Extra)
                    {
                        title="附加险";
                    }else
                    {
                        throw new Exception("未指定保单类型");
                    }
                }
                return title += "   ";
            }
        }

        public bool IsSelected
        {
            get { return this.isSelected; }
            set
            {
                this.isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        public bool IsCloseBtnEnabled
        {
            get { return this.isEnabled; }
            set
            {
                this.isEnabled = value;
                OnPropertyChanged("IsCloseBtnEnabled");
            }
        }

        //public RadTabControl Tab { get { return this.tab; } }

        public PolicyBaseControl ContentControl { get { return contentControl; } }

        public bool Verify()
        {
            return contentControl.Verify();
        }

        public DelegateCommand DelCommand { get; set; }


        public void OnSchemeChanged()
        {
            if (this.contentControl.PolicyLight.Owner.IsCheck == !this.isEnabled)
            {
                this.IsCloseBtnEnabled = this.contentControl.PolicyLight.Owner.IsCheck;
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
